#include "Avalon/IO/TaskWriter.hpp"
#include "Avalon/IO/Buffer.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::Threading;
using namespace boost;
using namespace boost::posix_time;
using namespace std;
/*
class TaskWriter::WriterTask : public Task {
  public:

    WriterTask(TaskWriter* writer)
        : m_writer(writer) {}

    virtual ~WriterTask() {}

  protected:

    virtual void ProcessTask() {
      vector<pair<Buffer, AsyncOwner<void>::Ptr> > pendingWrites;
      {
        lock_guard<mutex> lock(m_writer->m_mutex);
        std::swap(pendingWrites, m_writer->m_pendingWrites);
      }
      for(vector<pair<Buffer, AsyncOwner<void>::Ptr> >::const_iterator i =
          pendingWrites.begin(); i != pendingWrites.end(); ++i) {
        Async<void>::Ptr writeResult = m_writer->m_destination->Write(i->first);
        writeResult->Wait(pos_infin);
        if(writeResult->GetState() == BaseAsync::EXCEPTION) {
          i->second->CopyException(writeResult);
        } else {
          i->second->SetComplete();
        }
      }
    }

  private:
    TaskWriter* m_writer;
};
*/
TaskWriter::TaskWriter(Carry<Writer> destination, ThreadPool& threadPool)
    : m_destination(destination),
      m_threadPool(threadPool) {}

TaskWriter::~TaskWriter() {}

void TaskWriter::Write(const void* data, int size, Out<Async<void> > result) {
  lock_guard<mutex> lock(m_mutex);
  m_pendingWrites.push_back(PendingWrite(data, size, result.Get()));
}

void TaskWriter::Write(const Buffer& data, Out<Async<void> > result) {
  lock_guard<mutex> lock(m_mutex);
  m_pendingWrites.push_back(PendingWrite(data, result.Get()));
}
